package cn.admin.configcenter.registry.server;

import cn.admin.config.common.protocol.ProtocolPacketCodec;
import cn.admin.config.common.protocol.SwizzleCodec;
import cn.admin.configcenter.registry.IOCFactory;
import cn.admin.configcenter.registry.service.ServerUtil;
import cn.admin.configcenter.registry.service.client.ConfigServerConnectionFactory;
import cn.admin.remoting.Remoting;
import com.caucho.hessian.io.SerializerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ConfigServer {

    private static ConfigServer startup;

    private ConfigServer() throws Exception {
        ExecutorService configServerTP = Executors.newCachedThreadPool(r -> {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        });
        ServerUtil.setConfigServerTP(configServerTP);
        SerializerFactory sf = Remoting.serialUtil().getHessianSerializerFactory();
        sf.addFactory(new ProtocolPacketCodec());
        sf.addFactory(new SwizzleCodec());
        ConfigServerConnectionFactory normalServer = IOCFactory.getConfigServerConnectionFactory();
        normalServer.start();
    }

    public static synchronized ConfigServer startup() throws Exception {

        if (startup == null) {
            startup = new ConfigServer();
        }
        return startup;
    }


}
